﻿// Copyright (c) 2008 screamingtarget@gmail.com

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.


using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;


namespace NfoViewer
{
   class RichTextBoxEx : RichTextBox
   {
      public Size TextSize
      {
         get { return CalcTextSize(); }
      }

      public WrapType WrapType
      {
         get { return _wrapType; }
         set
         {
            if ( _wrapType == value )
            {
               return;
            }

            _wrapType = value;

            switch ( _wrapType )
            {
               case WrapType.NoWrap:
                  WordWrap = false;
                  break;

               case WrapType.CharacterWrap:
                  WordWrap = false;
                  break;
                  
               case WrapType.WordWrap:
                  WordWrap = true;
                  break;
            }
         }
      }

      public void AdjustRightMargin()
      {
         if ( WrapType == WrapType.CharacterWrap )
         {
            RightMargin = ClientRectangle.Right;
         }
      }

      protected override void OnFontChanged( EventArgs e )
      {
         base.OnFontChanged( e );
         _charSize = CharacterSize();
      }

      protected override void OnHandleCreated( EventArgs e )
      {
         base.OnHandleCreated( e );
         _charSize = CharacterSize();
      }

      protected override void OnLinkClicked( LinkClickedEventArgs e )
      {
         base.OnLinkClicked( e );
         Process.Start( e.LinkText );
      }

      protected override void OnSizeChanged( EventArgs e )
      {
         base.OnSizeChanged( e );
         AdjustRightMargin();
      }

      private Size CalcTextSize()
      {
         Size sizeText = new Size();

         if ( _charSize != null &&
              _charSize.Width != 0 &&
              _charSize.Height != 0 )
         {
            Size sizeClient = ClientSize;

            sizeText.Width = ( sizeClient.Width - 2 ) / _charSize.Width;
            sizeText.Height = sizeClient.Height / _charSize.Height;
         }

         return sizeText;
      }

      private Size CharacterSize()
      {
         TextFormatFlags textFlags = TextFormatFlags.ExpandTabs |
            TextFormatFlags.NoPrefix | TextFormatFlags.WordBreak |
            TextFormatFlags.NoPadding;

         Size charSize;

         using ( Graphics g = Graphics.FromHwnd( Handle ) )
         {
            charSize = TextRenderer.MeasureText( g,
               " ", Font, Size, textFlags );
         }

         return charSize;
      }


      Size _charSize;
      WrapType _wrapType;
   }
}
